package com.yyl.fmall.shopcart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyl.fmall.product.entity.Product;
import com.yyl.fmall.product.entity.ProductImg;
import com.yyl.fmall.product.entity.ProductSku;
import com.yyl.fmall.product.service.IProductImgService;
import com.yyl.fmall.product.service.IProductService;
import com.yyl.fmall.product.service.IProductSkuService;
import com.yyl.fmall.shopcart.entity.ShoppingCart;
import com.yyl.fmall.shopcart.mapper.ShoppingCartMapper;
import com.yyl.fmall.shopcart.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车  服务实现类
 * </p>
 *
 * @author YuanSir
 * @since 2022-11-08
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    
    @Autowired
    private IProductService productService;
    
    @Autowired
    private IProductSkuService productSkuService;
    
    @Autowired
    private IProductImgService productImgService;
    
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ShoppingCart> getList(Integer userId) {
    
        List<ShoppingCart> shoppingCartList = list(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId, userId));
        
        populateShoppingCartList(shoppingCartList);
    
        return shoppingCartList;
    }
    
    
    @Override
    public List<ShoppingCart> listByCids(String cids) {
    
        List<String> cartIds = Arrays.asList(cids.split(","));
    
        List<ShoppingCart> shoppingCartList = list(new LambdaQueryWrapper<ShoppingCart>()
                .in(ShoppingCart::getCartId, cartIds));
    
        populateShoppingCartList(shoppingCartList);
    
        return shoppingCartList;
    
    }
    
    public void populateShoppingCartList(List<ShoppingCart> shoppingCartList) {
        
        List<String> productIdList = shoppingCartList.stream()
                .map(ShoppingCart::getProductId)
                .distinct()
                .collect(Collectors.toList());
        
        List<String> productSkuIdList = shoppingCartList.stream()
                .map(ShoppingCart::getSkuId)
                .collect(Collectors.toList());
        
        
        //根据ProductId查询出对应Img信息的map
        Map<String, String> productImgUrlMap = productImgService.list(new LambdaQueryWrapper<ProductImg>()
                        .in(productIdList.size() > 0, ProductImg::getItemId, productIdList)
                        .select(ProductImg::getItemId, ProductImg::getUrl)
                        .eq(ProductImg::getIsMain, 1))
                .stream()
                .collect(Collectors.toMap(ProductImg::getItemId, ProductImg::getUrl));
        
        Map<String, String> productIdNameMap = productService.list(new LambdaQueryWrapper<Product>()
                        .select(Product::getProductId, Product::getProductName)
                        .in(productIdList.size() > 0, Product::getProductId, productIdList))
                .stream()
                .collect(Collectors.toMap(Product::getProductId, Product::getProductName));
        
        Map<String, ProductSku> productSkuMap = productSkuService.list(new LambdaQueryWrapper<ProductSku>()
                        .select(ProductSku::getSkuId, ProductSku::getStock, ProductSku::getOriginalPrice, ProductSku::getSkuName, ProductSku::getSellPrice)
                        .in(productSkuIdList.size() > 0, ProductSku::getSkuId, productSkuIdList))
                .stream()
                .collect(Collectors.toMap(ProductSku::getSkuId, productSku -> productSku));
        
        shoppingCartList.forEach(
                shoppingCart -> {
                    
                    ProductSku productSku = productSkuMap.get(shoppingCart.getSkuId());
                    
                    shoppingCart.setProductName(productIdNameMap.get(shoppingCart.getProductId()));
                    shoppingCart.setSkuName(productSku.getSkuName());
                    
                    shoppingCart.setOriginalPrice(productSku.getOriginalPrice());
                    shoppingCart.setProductImg(productImgUrlMap.get(shoppingCart.getProductId()));
                    shoppingCart.setStock(productSku.getStock());
                    shoppingCart.setSellPrice(productSku.getSellPrice());
                    
                }
        );
        
    }
    
}
